<?php

error_reporting(E_ALL);
ini_set('display_errors', '1');

require_once dirname(__FILE__).'/database.class.php';
$db=new stdb();
$path='';
$a = session_id();if(empty($a)) session_start();


if(isset($_SESSION["root_path"]))
$path=$_SESSION["root_path"];

function is_logat()  //////////////// verifica daca esti logat 
{
    
    if(!isset($_SESSION['user']) || !isset($_SESSION['pass']))
        return 0;
    
    if( !isset($_COOKIE['pu']))
        return 0;
        
    else
    {   
        $user = substr($_COOKIE['pu'],0,32);
        $pass = substr($_COOKIE['pu'],32);
        
        if(strcmp($_SESSION['pass'],$pass)==0 && strcmp(md5(md5($_SESSION['user'])), $user)==0)
            return 1;
        else 
            return 0;
    }
    
}

function verifica_informatii($name,$parola,$an,$tel)
 {
    global $db;
    $sqlcommand="Select username from utilizatori where username='".$name."'";
    $rezultat = $db->q($sqlcommand);
    $nr = mysql_num_rows($rezultat);
    if($nr != 0)
        return 0;
    
    if(strlen($parola) < 2)
        return 0;

    if($an < 1 && $an > 6)
        return 0;

    if(strlen($tel) != 10)
        return 0;
    
    return 1;
 }
 
 function verifica_username($name,$parola){
   global $db;
    $sqlcommand="Select username from utilizatori where username='".$name."'";
    $rezultat = $db->q($sqlcommand);
    $nr = mysql_num_rows($rezultat);
    if($nr != 0)
    return 0;  
    if(strlen($parola) < 2)
    return 0;
    return 1;
 }

function pt_rezervari($n_tab,$n_form,$pre='') 
                {
  ////// creaza selctul pentru pagina de rezervari, @param1 - tabela de unde facem selectul
    ///////  @param2 ce name sa pune la select
    global $db;
    $result=$db->q("SELECT * FROM  `$n_tab`");
    $perioade='<select name="'.$n_form.'">';
    $perioade.='<option value="" ></option>';
                    while($row = mysql_fetch_array($result)){
                       $perioade.='<option value="'.$row[0].'"'; 
                       if(!strcmp($row[0],$pre)) $perioade.=' selected ';
                       $perioade.= '> '.$row[1].'</option>';
                        
                    }
    $perioade.='</select>';      
    
return $perioade;    
}

function user_head($adm=0){
       global $path;
    //////////////////////////// FAce partea de sus a paginii //////////////
    $logat=  is_logat();
    echo '<div id="user_head">';
    if(!$logat){
            if(!isset($_GET["actiune"])) $_GET["actiune"]=''; 
            switch($_GET['actiune'])
            {
               case '':                                    
                       echo '<a href="login.php" class="green-button pcb">
				<span>Login</span>
			</a>  
                              <a href="cont_nou.php" class="green-button pcb">
				<span>Inregistrare</span>
			</a>  
                                
                              ' ; break;
               case 'logare': 
                  
                   header ("Location:login.php");
                      
                   break;
                   
//                  
             case 'cont_nou': header ("Location:cont_nou.php");
                 
                          
            }
            }
            else {
               ?>

<a href="<?echo $path; ?>contul_meu.php" class="green-button pcb"><span>Contul Meu</span></a>  
<a href="<?echo $path; ?>cauta.php?actiune=delogare" class="green-button pcb"><span>Log out</span>	</a>  
               <? 
           
            }
       echo '</div>';
    
}

function header_out($adm=0){
  ?>
 
    <div id="header">
		<div id="header_inside">

        <?php
            global $path;
            if($adm){
                         echo '<a href="'.$path.'boss/admin.php" class="green-button pcb">
				<span>Panou Admin</span>
                                </a> ';  
                       }
            user_head($adm);

        ?>

                </div>
        </div>
    <?
    
    
}

function header_login(){
   global $path; ?>
        <div id="header">
		<div id="header_inside">
        <div id="user_head">

         Utilizator: <input type="text" name="username" id="username" value=""/> 
         Parola:     <input type="password" name="password" id="password" value=""/>
         <a href="#" class="grey-button pcb"><span><input type="Submit" value="submit" id="log_in"></input></span></a><a href="<?echo $path; ?>cont_nou.php" class="grey-button pcb"><span>Cont Nou</span></a> <br/> <br/>

            </div>
            </div>
        </div>

<?

if( session_destroy() == true )
            setcookie("pu", '', time()-3600, '/');
}

function what_header(){
    global $path;
    if(!isset($_SESSION['link_list'])){ $_SESSION['link_list']=array(); $_SESSION['link_list'][0]='index.php';   }
    else
    if(strcmp($_SERVER['REQUEST_URI'],$_SESSION['link_list'][0]))
    array_unshift($_SESSION['link_list'],$_SERVER['REQUEST_URI']); 
    
    ?>
        <script src="<?echo $path; ?>js/jquery.min_1.js" type="text/javascript"></script> 
        <script src="<?echo $path; ?>js/jquery-ui.min.js" type="text/javascript"></script>
	<script src="<?echo $path; ?>js/jquery.ui.core.js"></script>
	<script src="<?echo $path; ?>js/jquery.ui.widget.js"></script>
	<script src="<?echo $path; ?>js/jquery.ui.datepicker.js"></script>
	<link rel="stylesheet" href="<?echo $path; ?>js/datepick.css">
        <link rel="stylesheet" type="text/css" href="<?echo $path; ?>js/jquery-ui.css" />
	
	<script type="text/javascript">
	$(function() {$( "#datepicker" ).datepicker({ dateFormat: 'yy-mm-dd' });});
	</script>
        <?  if(strlen(substr($_SERVER['REQUEST_URI'],0,strrpos($_SERVER['REQUEST_URI'],'/')).'/')>strlen($path) && $path!==''){?>
        <script src="<?echo $path; ?>js/prepend.js" type="text/javascript"></script>
        <? } else {?>
        <script src="<?echo $path; ?>js/prepend0.js" type="text/javascript"></script>
        <? }?>
        <script src="<?echo $path; ?>js/jquery.popup.js" type="text/javascript"></script>
        <script src="<?echo $path; ?>js/js_rezerv.js" type="text/javascript"></script>
        <script src="<?echo $path; ?>js/jsfunct.js" type="text/javascript"></script>
        
        <?
    
    $logat = is_logat();
    if($logat){  ///// Afisam header-ul corespunzator 
        $adm=0;
        if( $_SESSION['logat'] == "admin" )
        $adm=1;
        else
        $_SESSION["root_path"]=substr($_SERVER['REQUEST_URI'],0,strrpos($_SERVER['REQUEST_URI'],'/')).'/';
        
        header_out($adm);
        
        return 1;
        }
        else
        {
            header_login();
            return 0;
            }
    
}

function footer($adm=0){
    global $path;
    ?>
    <div id="footer">
			<div id="footer_inside">
				
                        </div>
		</div>
        
    <?
}

function list_tel($adm=0){
    global $db,$path;
    echo "<ul style='float:center;'>";
    $sql_cauta = "SELECT * FROM carte_telefon ";
    $rezultat = $db->q($sql_cauta);
        if(!$rezultat)
            echo "Nu exista acest nume/nr tel in cartea de telefon";
        else
        {
            while( $row =  mysql_fetch_array($rezultat) )
                echo "<li style='margin-left:20px;'><a href='".$path."profil.php?id=".$row['user_id']."'>".$row['nume']."</a>       ".$row['telefon']."</li><br/>";

        }
        echo "</ul>";

}

function list_meta($fid,$adm=0){
    global $db;
    $show="";
    $sql_cauta="SELECT meta_list.metan FROM `file_meta` INNER JOIN meta_list ON meta_list.MID=file_meta.MID WHERE file_meta.FID=$fid";
    $rezultat = $db->q($sql_cauta);
       if(!$rezultat)
           $show.= "NO META :|";
        else
        {
          $show.=" ";
          if($adm)
           while( $row =  mysql_fetch_array($rezultat) )
            {
               for($i=0;$i<(count($row,1)-1);$i++){
                  $show.='<span class="meta" id="fidul_'.$fid.'" title="Shift+click to remove!">'.$row[$i].'</span>, '; 
                   
               }
           }
           else
               while( $row =  mysql_fetch_array($rezultat) )
            {
               for($i=0;$i<(count($row,1)-1);$i++){
                  $show.=''.$row[$i].', '; 
                   
               }
           }
          
          $show.=" ";
        }
    return $show;
        
}
function list_downloads($adm=0){
    global $db,$path;
    $show="";
    $sql_cauta="SELECT file_table.FID,file_table.nume,categorii.categorie,ani.an,file_table.descriere,file_table.type,utilizatori.username,file_table.data FROM file_table INNER JOIN categorii ON file_table.CAT=categorii.CID INNER JOIN ani ON file_table.AN=ani.AID INNER JOIN utilizatori ON file_table.UID=utilizatori.ID WHERE FID>0 ";
    if($adm<1)
        $sql_cauta.=" AND status=1 ";
    $show.= "<table style='width:550px;'>";
    $rezultat = $db->q($sql_cauta);
        if(!$rezultat)
           $show.= "N-am gasit nimic!  :(";
        else
        {
            $show.="<tr>";
                    $show.="<td >";
                    $show.="ID";
                    $show.="</td>";
                    $show.="<td >";
                    $show.="Nume";
                    $show.="</td>";
                    $show.="<td >";
                    $show.="Categorie";
                    $show.="</td>";
                    $show.="<td >";
                    $show.="An";
                    $show.="</td>";
                    $show.="<td >";
                    $show.="Descriere";
                    $show.="</td>";
                    $show.="<td >";
                    $show.="Tipul";
                    $show.="</td>";
                    $show.="<td >";
                    $show.="Uploader";
                    $show.="</td>";
                    $show.="<td >";
                    $show.="data";
                    $show.="</td>";
                
           $show.="</tr>";
            
            
            while( $row =  mysql_fetch_array($rezultat) )
            {
                $show.="<tr>";
                for($i=0;$i<8;$i++){
                    $elem=$row[$i];
                    $show.="<td >";
                    if($i==0) {$fid = $row[$i];$title=list_meta($fid);}
                    if($i==1) $show.= "<a href='".$path."down_show.php?id=$fid' tile='$title' >";
                    $show.="$elem";
                    if($i==1) $show.= "</a>";
                    $show.="</td>";
                }
                $show.="</tr>";
            }

        }
        
    
    $show.= "</table>";
    return $show;
}

function list_coms($fid){
    global $db;
    $show="";
    $sql_cauta="SELECT comentarii.COM,comentarii.data,utilizatori.username FROM comentarii INNER JOIN utilizatori ON utilizatori.ID=comentarii.UID WHERE FID=$fid";
    $rezultat = $db->q($sql_cauta);
    if(!$rezultat)
           $show.= "N-am gasit nimic!  :( Fii primul care comenteaza.";
        else
        {
            while( $row =  mysql_fetch_array($rezultat) ){
                $show.="<h4> ".$row[2].", pe data de ". $row[1] . " a afirmat :</h4> ";
                $show.='<textarea class="com" style="width:550px;border:solid lightgray;" readonly="readonly">';
                 $show.= $row[0] . "";
                $show.='</textarea>';
            }
                
                
            
        }
    return $show;
}

function list_rez($adm=0){
        global $db,$path;
        mysql_safer();
        $query="SELECT rezervari.RID,sali.nume,perioade.perioada,rezervari.data ,detalii_utilizatori.Nume,detalii_utilizatori.Prenume,rezervari.motiv FROM `rezervari` INNER JOIN `sali` ON rezervari.OID=sali.SID INNER JOIN `perioade` ON rezervari.PID=perioade.PID INNER JOIN `detalii_utilizatori` ON rezervari.UID=detalii_utilizatori.ID WHERE RID>0";
       if($adm<1)
        $query.=" AND STATUS=1 ";
        
        $search="<div>";
        if(isset($_SESSION['userc']))
        $user=unserialize($_SESSION['userc']);
        
        $query.="  ORDER BY data DESC";
        $result=$db->q($query);
        $search.= "<table border='1' class='p_rezervari_table'>";
        $search.= "<tr border='1'>";
                $search.=  "<td style='width:16%;'>";
                $search.=  "Sala";
                $search.=  "</td>";
                $search.=  "<td style='width:16%;'>";
                $search.=  "Interval Orar";
                $search.=  "</td>";
                $search.=  "<td style='width:16%;'>";
                $search.=  "Data";
                $search.=  "</td>";
                $search.=  "<td style='width:16%;'>";
                $search.=  "Nume";
                $search.=  "</td>";
                $search.=  "<td style='width:16%;'>";
                $search.=  "Prenume";
                $search.=  "</td>";
                $search.=  "<td style='width:16%;'>";
                $search.=  "Motiv";
                $search.=  "</td>";
          
        $search.=  "</tr>";
        $search.= "</table>";

        $search.= "<ul class='p_rezerveri_list'>";
        if(isset($result) && !empty($result) )
        while($rows = mysql_fetch_array($result)){
            $search.= "<li id='p_".$rows[0]."' class='p_rezervari_li' >";

            for($i=1;$i<7;$i++){
                $search.=  " <span>";
                $search.=  $rows[$i];
                $search.=  "</span> ";
            }
                    if(isset($_SESSION['logat']) && $_SESSION['logat'] == "admin" )
    {
            $search.= '<div class="p_rezervari_extra" style="display:none;">';
            $search.= '<a href="'.$path.'boss/e_rezervari.php?r='.$rows[0] .'" class="edit" id="e_'.$rows[0] .'">Edit</a>';
            $search.= '<a href="#" class="remove" id="r_'.$rows[0] .'">Remove</a>';
            $search.= '</div>';
            $search.=  "</li>";
    } 
                   }
          $search.= "</ul>";
   
        
         $search.= "</div>";
      
       
        return $search;
    
    
    
}

function list_news($nr=0){
    global $db,$path;
    $show="";
    $sql_cauta="SELECT news.TITLU,news.CONTINUT,news.DATA,utilizatori.username,news.NID FROM news INNER JOIN utilizatori ON utilizatori.ID=news.UID ORDER BY NID DESC LIMIT ".$nr.",".($nr+5)." ";
    $rezultat = $db->q($sql_cauta);
    if(!$rezultat)
           $show.= "N-am gasit nimic!  :( .";
        else
        {
            while( $row =  mysql_fetch_array($rezultat) ){
                $show.="<br><h1><a href='".$path."news_show.php?action=show&id=".$row[4]."'> ".$row[0]."</a></h1>";
                $show.='<blockquote><p>';
                 $show.= $row[1] . "";
                $show.='</p></blockquote>';
                $show.="<br><p> pe data de ". $row[2] . " de " . $row[3] . " </p>" ;
            }
                
                
            
        }
    return $show;
}
?>


